package com.xt.purchasingsystem.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xt.purchasingsystem.model.SupplierFile;
import com.xt.purchasingsystem.service.SupplierFileService;
import com.xt.purchasingsystem.utils.ExportExcel;
import com.xt.purchasingsystem.utils.IdWorker;
import com.xt.purchasingsystem.utils.Result;
import com.xt.purchasingsystem.utils.ResultFactory;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * 供应商档案管理所有接口
 * </p>
 *
 * @author yhp
 * @since 2020-05-31
 */
@RestController
@RequestMapping("/supplierFile")
public class SupplierFileController {
    @Autowired
    private SupplierFileService supplierFileService;
    @Autowired
    private IdWorker idWorker;

    /**
     * 多条件分页查询所有已审核的供应商档案
     *
     * @return
     */
    @PostMapping("/{currNo}/{pageSize}")
    public Result getAllSupplierFileIsCheckByPage(@PathVariable Integer currNo, @PathVariable Integer pageSize, @RequestBody SupplierFile supplierFile) {
        QueryWrapper<SupplierFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("delete_tag", "C001-0");
        queryWrapper.eq("check_tag", "R001-1");
        if (StringUtils.isNotEmpty(supplierFile.getFirstKindId())) {
            queryWrapper.eq("FIRST_KIND_ID", supplierFile.getFirstKindId());
        }
        if (StringUtils.isNotEmpty(supplierFile.getSecondKindId())) {
            queryWrapper.eq("SECOND_KIND_ID", supplierFile.getSecondKindId());
        }
        if (StringUtils.isNotEmpty(supplierFile.getFirstKindId())) {
            queryWrapper.eq("THIRD_KIND_ID", supplierFile.getThirdKindId());
        }

        IPage<SupplierFile> page = supplierFileService.page(new Page<>(currNo, pageSize), queryWrapper);
        return ResultFactory.buildSuccessResult("查询成功", page);
    }

    /**
     * 根据供应商名称查询供应商信息的接口
     *
     * @param supplierFileName
     * @return
     */
    @GetMapping("/getSupplierFileBySupplierFileName/{supplierFileName}")
    public Result getSupplierFileBySupplierFileName(@PathVariable String supplierFileName) {
        SupplierFile supplierFile = supplierFileService.getOne(new QueryWrapper<SupplierFile>().eq("supplier_name", supplierFileName));
        if (supplierFile != null) {
            return ResultFactory.buildSuccessResult("查询成功", supplierFile);
        } else {
            return ResultFactory.buildFailResult("查询失败");
        }

    }

    /**
     * 添加供应商档案
     *
     * @param supplierFile
     * @return
     */
    @PostMapping
    public Result add(@RequestBody SupplierFile supplierFile) {
        String id = String.valueOf(idWorker.nextId());
        supplierFile.setId(id);
        boolean flag = supplierFileService.save(supplierFile);
        if (flag) {
            return ResultFactory.buildSuccessResult("添加成功", id);
        } else {
            return ResultFactory.buildFailResult("添加失败");
        }

    }

    /**
     * 供应商档案复核
     *
     * @param supplierFile
     * @return
     */
    @PutMapping("supplierfileReview")
    public Result supplierfileReview(@RequestBody SupplierFile supplierFile) {
        boolean flag = supplierFileService.saveOrUpdate(supplierFile);
        if (flag) {
            return ResultFactory.buildSuccessResult("复核成功");
        } else {
            return ResultFactory.buildFailResult("复核失败");
        }
    }

    /**
     * 供应商档案永久删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteSupplierFile(@PathVariable String id) {
        boolean flag = supplierFileService.removeById(id);
        if (flag) {
            return ResultFactory.buildSuccessResult("删除成功");
        } else {
            return ResultFactory.buildFailResult("删除失败");
        }
    }

    /**
     * 查询供应商档案
     */
    @GetMapping("procurementRegistrationReview/{currNo}/{pageSize}")
    public Result procurementRegistrationReview(@PathVariable Integer currNo, @PathVariable Integer pageSize) {
        IPage<SupplierFile> page = supplierFileService.page(new Page<>(currNo, pageSize), new QueryWrapper<SupplierFile>().eq("check_tag", "R001-0").eq("delete_tag", "C001-0"));
        return ResultFactory.buildSuccessResult("查询成功", page);
}

    /**
     * 查询供应商档案
     */
    @GetMapping("getAllSupplierFileByProductName/{productId}/{currNo}/{pageSize}")
    public Result getAllSupplierFileByProductName(@PathVariable String productId,@PathVariable Integer currNo,@PathVariable Integer pageSize) {
        return supplierFileService.getAllSupplierFileByProductName(productId,currNo,pageSize);
    }

    /**
     * 根据供应商编号查询此供应商7天内的已采购数量,已放货数量，已合格数量
     * @return
     */
    @GetMapping("/{supplierId}")
    public Result getDataDrawingListBySupplierId(@PathVariable String supplierId){
       return supplierFileService.getDataDrawingListBySupplierId(supplierId);
    }

    /**
     * Excel 批量导入供应商的接口
     * @param supplierFiles
     * @return
     */
    @PostMapping("/readExcel")
    public Result readExcel(@RequestBody List<SupplierFile> supplierFiles){
        return supplierFileService.readExcel(supplierFiles);
    }

    /**
     * 供应商档案导出
     */
    @GetMapping("/excel")
    public Result excel(HttpServletResponse response) throws IOException {

        //获取所有的数据
        ExportExcel<SupplierFile> ee = new ExportExcel<>();
        QueryWrapper<SupplierFile> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("check_tag","R001-1").eq("delete_tag","C001-0");
        List<SupplierFile> supplierFileList = supplierFileService.list(queryWrapper);
        String fileName = "供应商档案表";
        String[] headers = {"供应商名称", "地址", "产品I级分类编号","产品I级分类名称","产品II级分类编号", "产品II级分类名称"
                , "产品III级分类编号","产品III级分类名称", "供应商曾用名", "开户银行", "银行账户", "网址", "电话", "传真", "邮政编码"
                ,"所在区域编号", "所在区域名称","优质级别编号", "优质级别名称", "第一联系人", "第一联系人部门", "第一联系人职务", "第一联系人办公电话", "第一联系人手机",
                "第一联系人家庭电话", "第一联系人邮箱", "第一联系人性别","第二联系人", "第二联系人部门", "第二联系人职务", "第二联系人办公电话", "第二联系人手机",
                "第二联系人家庭电话", "第二联系人邮箱", "第二联系人性别", "开票信息","可供应产品集合", "推荐供应产品集合","登记人编号", "登记人", "登记时间",
                "采购人编号","采购人","审核人","审核时间","审核状态","变更人","变更时间","变更状态","删除状态"};

        ee.exportExcel(headers, supplierFileList, fileName, response);
        return ResultFactory.buildSuccessResult("导入成功");

    }


}

